import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import zhtt.SessionFactoryUtil;
import zhtt.demo01.Blog;
import zhtt.demo01.BlogMapper;

/**
 * Created by mengxiaxia on 2017/4/25.
 */
public class BlogDao {

    @Test
    public void save(){
        SqlSession sqlSession = null;
        try {
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog = new Blog("herry3","service3");
            int id = sqlSession.insert("zhtt.demo01.Blog.saveBlog", blog);
            System.out.println(id);
            System.out.println(blog.getId());

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void selectBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=sqlSession.selectOne("zhtt.demo01.Blog.selectBlog",1);
            System.out.println(blog);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }


    @Test
    public void get(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            Blog blog=sqlSession.selectOne("zhtt.demo01.Blog.get",1);
            System.out.println(blog);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }

    @Test
    public void selectBlog2(){
        SqlSession sqlSession=null;
        try{
            sqlSession = SessionFactoryUtil.getSqlSessionIntance();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            Blog blog = mapper.selectBlog(1);
            System.out.println(blog);
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if(sqlSession!=null)
                sqlSession.close();
        }
    }
}
